package org.deltaset.meta.faces;

import java.util.Collection;
import java.util.List;
import javax.faces.component.NamingContainer;
import javax.faces.component.UIComponentBase;
import org.deltaset.faces.FacesUtils;
import org.deltaset.meta.CollectionHandler;
import org.deltaset.meta.EntityResolver;
import org.deltaset.meta.MetaField;

/**
 *
 * @author dmakariev
 */
public class MetaCollectionNamingContainer extends UIComponentBase implements NamingContainer {

    private MetaCrudNamingContainer metaCrudNamingContainer;
    private CollectionHandler collectionHandler;
    private EntityResolver entityResolver;
    private String sourceIndex;
    private String targetIndex;

    public String getSourceIndex() {
        return sourceIndex;
    }

    public void setSourceIndex(String sourceIndex) {
        this.sourceIndex = sourceIndex;
    }

    public String getTargetIndex() {
        return targetIndex;
    }

    public void setTargetIndex(String targetIndex) {
        this.targetIndex = targetIndex;
    }

    @Override
    public String getFamily() {
        return "javax.faces.NamingContainer";
    }

    public boolean isRemovableCollection(final MetaField metaField) {
        return this.getEntityResolver().isRemovableCollection(metaField);
    }

    public boolean isOrderableCollection(final MetaField metaField) {
        return this.getEntityResolver().isOrderableCollection(metaField);
    }

    public void moveTo(final List list) {
        try {

            final int source = Integer.valueOf(getSourceIndex());
            final int target = Integer.valueOf(getTargetIndex());
            this.getCollectionHandler().moveTo(list, source, target);
        } catch (NumberFormatException e) {
        }
    }

    public void moveUp(final List list, final int currentIndex) {
        getCollectionHandler().moveUp(list, currentIndex);
    }

    public void moveDown(final List list, final int currentIndex) {
        getCollectionHandler().moveDown(list, currentIndex);
    }

    public void removeElement(Collection collection, Object element) {
        getCollectionHandler().removeElement(collection, element);
    }

    public List convertToList(Collection collection) {
        return getCollectionHandler().convertToList(collection);
    }

    private EntityResolver getEntityResolver() {
        if (null == entityResolver) {
            entityResolver = getMetaCrudNamingContainer().getEntityResolver();
        }
        return entityResolver;
    }

    private CollectionHandler getCollectionHandler() {
        if (null == collectionHandler) {
            collectionHandler = getMetaCrudNamingContainer().getCollectionHandler();
        }
        return collectionHandler;
    }

    private MetaCrudNamingContainer getMetaCrudNamingContainer() {
        if (null == metaCrudNamingContainer) {
            metaCrudNamingContainer = FacesUtils.getAttribute(this, "meta", MetaCrudNamingContainer.class);
        }
        return metaCrudNamingContainer;
    }
}
